//
//  LRWelcomeViewController.swift
//  LRWeibo
//
//  Created by admin on 15/9/25.
//  Copyrig ht © 2015年 懒人. All rights reserved.
//

import UIKit
import SDWebImage

class LRWelcomeViewController: UIViewController {
    
     // MARK: - 动画出现效果
    /// 定义imageview的约束
    private var imageViewBottom: NSLayoutConstraint?
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 删除约束
        view.removeConstraint(imageViewBottom!)
        
        // 重新添加
        view.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: 160))

        // 更新
        view.updateConstraints()
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            // 重新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
        
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    
                    self.msgLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        // 发送通知 展现主控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(LRRootViewControllChange, object: self)
                })
        }
    }
    
     // MARK: - 初始化控件
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        view = UIImageView(image: UIImage(named: "ad_background"))
        view.addSubview(imageView)
        view.addSubview(msgLabel)
        
        imageView.sd_setImageWithURL(NSURL(string: UserAccountViewModel().avatar_large!))
        
        for v in view.subviews
        {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        imageViewBottom = NSLayoutConstraint(item: imageView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: -160)
        
        view.addConstraint(NSLayoutConstraint(item: imageView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(imageViewBottom!)
        imageView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        msgLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(imageView.snp_centerX)
            make.top.equalTo(imageView.snp_bottom).offset(16)
        }
        msgLabel.alpha = 0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
     // MARK: - 控件懒加载
    /// 头像
    private lazy var imageView: UIImageView = {
    
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius = 45
        icon.layer.masksToBounds = true
        return icon
    }()
    
    /// 文字
    private lazy var msgLabel: UILabel = UILabel(title: "欢迎归来", color: UIColor.cyanColor(), fontSize: 16)

}
